package com.project.class4_6;

/**
 * 线程同步的测试
 */
public class BankCode {
    /**
     * 创建一个初始金额
     */
    private  int money= 800;
    public synchronized void getMoney(int getMoney){
        if (getMoney>money){
            System.out.println("余额不足");
        }else {
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            this.money-=getMoney;
            System.out.println("取出金额：  "+getMoney);
            System.out.println("剩余金额：  "+this.money);
        }
    }

    public static void main(String[] args) {
        BankCode bankCode = new BankCode();
        User user1 = new User(bankCode,600);
        User user2 = new User(bankCode,500);
    }
}
class User implements Runnable{
    /**
     * 银行账号
     */
    private BankCode bankCode;
    //取款金额
    private int money;
    public User(BankCode code,int takeMoney){
        this.bankCode=code;
        this.money= takeMoney;
        new Thread(this).start();
    }
    @Override
    public void run() {
        this.bankCode.getMoney(money);
    }
}